package com.freestyle.jms.common;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * Created by rocklee on 2022/1/8 11:52
 */

public class RabbitMQConfig {

  @Configuration(
          proxyBeanMethods = false
  )
  @EnableConfigurationProperties({RabbitProperties.class})
  @ConditionalOnClass({RabbitTemplate.class, Channel.class})
  public static class ProducerConfig implements InitializingBean {
    //private NormalCachingConnectionFactory normalCachingConnectionFactory;
    @Resource
    private RabbitMQEvent rabbitMQEvent;
    @Resource
    private RabbitProperties rabbitProperties;

    //normalCachingConnectionFactory,自动应答的连接工厂，不能用注入的方式
   /* public NormalCachingConnectionFactory getNormalCachingConnectionFactory() {
      if (normalCachingConnectionFactory==null){
        normalCachingConnectionFactory=createNormalCachingConnectionFactory(rabbitProperties.getHost(),rabbitProperties.getPort(),
                rabbitProperties.getUsername(),rabbitProperties.getPassword());
      }
      return normalCachingConnectionFactory;
    }*/

    //手动应答的template
    @Bean
    //@Scope("prototype")
    public AckRabbitTemplate ackRabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory cachingConnectionFactory) {
      AckRabbitTemplate rabbitTemplate=new AckRabbitTemplate(cachingConnectionFactory);
      configurer.configure(rabbitTemplate, cachingConnectionFactory);
      rabbitTemplate.setMandatory(true); //设置手动ack
      rabbitTemplate.setChannelTransacted(false); //关闭事务
      rabbitTemplate.setConfirmCallback(rabbitMQEvent);
      rabbitTemplate.setReturnsCallback(rabbitMQEvent);
      ((AbstractMessageConverter)rabbitTemplate.getMessageConverter()).setCreateMessageIds(true);
      return rabbitTemplate;
    }
    //自动应答的template
    @Bean
    public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer,ConnectionFactory connectionFactory) {
      RabbitTemplate template = new RabbitTemplate();
      //configurer.configure(template, getNormalCachingConnectionFactory());
      configurer.configure(template,connectionFactory);
      ((AbstractMessageConverter)template.getMessageConverter()).setCreateMessageIds(true);
      return template;
    }
   public NormalCachingConnectionFactory createNormalCachingConnectionFactory(@Value("${spring.rabbitmq.host}")String host, @Value("${spring.rabbitmq.port}") int port,
                                                                         @Value("${spring.rabbitmq.username:}")String user, @Value("${spring.rabbitmq.password:}")String password){
      NormalCachingConnectionFactory connectionFactory=new NormalCachingConnectionFactory(host,port);
      if (StringUtils.hasText(user)){
        connectionFactory.setUsername(user);
      }
      if (StringUtils.hasText(password)){
        connectionFactory.setPassword(password);
      }
      return connectionFactory;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory cachingConnectionFactory){
      return new RabbitAdmin(cachingConnectionFactory);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
      //getNormalCachingConnectionFactory();
    }
  }


}
